import axios from "axios"
import qs from "qs"
import { MallProp } from "./types"
import { loginProp } from "@/pages/login/types"

// 获取商品列表
const getMall = async () => {
    return axios({
        method: 'GET',
        headers: { 'content-type': 'application/json', 'Authorization': localStorage.getItem('Authorization') },
        url: 'http://localhost:8080/api/mall'
    })
}

// 新增一个商品
const addMall = async (data: MallProp) => {
    return axios({
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': localStorage.getItem('Authorization') },
        data: qs.stringify(data),
        url: 'http://localhost:8080/api/addMall'
    })
}

// 根据id修改商品
const editMall = async (data: MallProp) => {
    return axios({
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': localStorage.getItem('Authorization') },
        data: qs.stringify(data),
        url: 'http://localhost:8080/api/editMall'
    })
}

// 根据id删除商品
const deleteMall = async (id: number) => {
    return axios({
        method: "DELETE",
        headers: { 'content-type': 'application/json', 'Authorization': localStorage.getItem('Authorization') },
        url: `http://localhost:8080/api/deleteMall/${id}`
    })
}

// 根据商品名称查询商品
const getMallByName = async (data: { name: string }) => {
    return axios({
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': localStorage.getItem('Authorization') },
        data: qs.stringify(data),
        url: 'http://localhost:8080/api/getMallByName'
    })
}

// 用户注册
const userReg = async (data: loginProp) => {
    return axios({
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        data: qs.stringify(data),
        url: 'http://localhost:8080/my/regUser'
    })
}

// 用户登录
const userLogin = async (data: { username: string, password: string }) => {
    return axios({
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        data: qs.stringify(data),
        url: 'http://localhost:8080/my/login'
    })
}

export { getMall, addMall, editMall, deleteMall, getMallByName, userLogin, userReg }